package cn.stylefeng.guns.modular.system.service;

import cn.stylefeng.guns.core.common.cons.GeneralTypeEnum;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.modular.system.entity.Car;
import cn.stylefeng.guns.modular.system.entity.CollectionCar;
import cn.stylefeng.guns.modular.system.entity.OrderCar;
import cn.stylefeng.guns.modular.system.mapper.CarMapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-12-07
 */
@Service
public class CarService extends ServiceImpl<CarMapper, Car> {


    /**
     * 根据条件查询车辆列表
     *
     */
    public List<Map<String, Object>> selectCars(String carType, String carColor) {
        return this.baseMapper.selectCars(carType,carColor);
    }

    /**
     * 添加车辆
     */
    @Transactional(rollbackFor = Exception.class)
    public void addCar(Car car){
        if (ToolUtil.isOneEmpty(car, car.getCarType(), car.getCarColor(), car.getCarPrice() , car.getCarInspection())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.save(car);
    }

    /**
     * 修改车辆信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void editCar(Car car) {
        if (ToolUtil.isOneEmpty(car, car.getCarType(), car.getCarColor(), car.getCarPrice() ,car.getCarInspection())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        this.updateById(car);
    }
    /**
     * 删除车辆
     */
    @Transactional
    public void deleteCar(Long carId) {
        Car car = this.baseMapper.selectById(carId);
        car.setIsDel(GeneralTypeEnum.IS_DELETE.getCode());
        this.baseMapper.updateById(car);
    }

    public void insertCollectionCar(CollectionCar collectionCar) {
        this.baseMapper.insertCollectionCar(collectionCar);
    }

    public List<Long> getMyCollectionCar(Long userId) {
        return this.baseMapper.getMyCollectionCar(userId);
    }

    public List<Map<String, Object>> getCarsById(List<Long> carIds) {
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        for (Long carId : carIds) {
            wrapper = wrapper.eq("CAR_ID",carId);
        }
        return this.baseMapper.selectMaps(wrapper);
    }

    public List<Map<String, Object>> selectCollectionCars(List<Long> carIds) {
        return this.baseMapper.selectCollectionCars(carIds);
    }

    public void insertOrderCar(OrderCar orderCar) {
        this.baseMapper.insertOrderCar(orderCar);
    }

    public List<Long> getMyOrderCar(Long userId) {
        return this.baseMapper.getMyOrderCar(userId);
    }
    public List<Map<String, Object>> selectOrderCar(List<Long> carIds) {
        return this.baseMapper.selectOrderCar(carIds);
    }

    public void deleteThisOrder(Long userId) {
        this.baseMapper.deleteThisOrder(userId);
    }
}
